package com.yssoft.service.handle;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yssoft.entity.core.TProcessingTechnology;
import com.yssoft.entity.core.TTechnicIndex;
import com.yssoft.mapper.core.TProcessingTechnologyMapper;
import com.yssoft.mapper.core.TTechnicIndexMapper;
import com.yssoft.shiro.MyRealm.ShiroUser;
import com.yssoft.utils.LayuiUtil;
import com.yssoft.utils.UserHelper;

/**
 * @author 戈超
 * @time 2018/5/23 下午 18:37 @ 工艺管理service
 */
@Service
public class ProcessService {

	@Resource
	private TProcessingTechnologyMapper tProcessingTechnologyMapper;
	@Resource
	private TTechnicIndexMapper tTechnicIndexMapper;

	/**
	 * 查询工艺管理信息
	 * @author 戈超
	 * @return
	 */
	public JSONObject queryProcessList(int page, int limit, String processName) {
		Integer entId = UserHelper.getCurrentCompanyId();
		PageHelper.startPage(page, limit);// 下面第一个查询结果进行分页
		Map<String, Object> param = new HashMap<>();
		param.put("processName", processName);
		param.put("deleteFlag", "0");
		param.put("tEId", entId);
		List<Map<String,Object>> list = tProcessingTechnologyMapper.queryProcessList(param);
		int count = tProcessingTechnologyMapper.queryCountSql(param);// 查询条数
		JSONObject jsonObject = LayuiUtil.getJSONObjectByMap(list, count, "", "");
		return jsonObject;
	}

	/**
	 * 保存工艺管理信息
	 * @author 戈超
	 * @param tProcessingTechnology
	 */
	public void processAddSave(TProcessingTechnology tProcessingTechnology) {
		if(tProcessingTechnology.getId() != null){
			tProcessingTechnologyMapper.updateByPrimaryKeySelective(tProcessingTechnology);
		} else {
			tProcessingTechnology.setDeleteFlag("0");
			tProcessingTechnology.setCreateTime(new Date());
			tProcessingTechnology.setCreatorId(UserHelper.getCurrentUser().getPersonId());
			tProcessingTechnology.setCreatorName(UserHelper.getCurrentUser().getPersonName());
			tProcessingTechnology.settEId(UserHelper.getCurrentCompanyId());
			tProcessingTechnologyMapper.insertSelective(tProcessingTechnology);
		}
	}

	/**
	 * 工艺管理信息删除
	 * @author 戈超
	 * @param processId
	 */
	public void delProcess(String processId) {
		TProcessingTechnology tProcessingTechnology = new TProcessingTechnology();
		tProcessingTechnology.setId(Integer.parseInt(processId));
		tProcessingTechnology.setDeleteFlag("1");
		tProcessingTechnologyMapper.updateByPrimaryKeySelective(tProcessingTechnology);
	}

	/**
	 * 工艺管理信息查询（根据Id）
	 * @param inspectId
	 * @return
	 */
	public TProcessingTechnology queryProcessView(Integer processId) {
		/*查询工艺管理信息*/
		TProcessingTechnology tProcessingTechnology = new TProcessingTechnology();
		tProcessingTechnology.setId(processId);
		tProcessingTechnology.setDeleteFlag("0");
		tProcessingTechnology.settEId(UserHelper.getCurrentCompanyId());
		TProcessingTechnology process = tProcessingTechnologyMapper.selectByPrimaryKey(tProcessingTechnology);
		/*查询工艺指标*/
		TTechnicIndex tTechnicIndex = new TTechnicIndex();
		tTechnicIndex.setT_p_id(processId);
		tTechnicIndex.setDeleteFlag("0");
		tTechnicIndex.settEId(UserHelper.getCurrentCompanyId());
		List<TTechnicIndex> technicIndexList = tTechnicIndexMapper.select(tTechnicIndex);
		/*Map<String, Object> param = new HashMap<>();
		param.put("tPId", processId);
		param.put("deleteFlag", "0");
		param.put("tEId", UserHelper.getCurrentCompanyId());
		List<Map<String,Object>> technicIndexList = tTechnicIndexMapper.queryTechnicIndexList(param);*/
		process.settTechnicIndexList(technicIndexList);
		return process;
	}

	/**
	 * 实时删除工艺指标
	 * @author 戈超
	 * @param delId
	 */
	public void deleteTechnic(Integer delId) {
		TTechnicIndex technicIndex = new TTechnicIndex();
		technicIndex.setDeleteFlag("1");
		technicIndex.setId(delId);
		tTechnicIndexMapper.updateByPrimaryKeySelective(technicIndex);
	}

	/**
	 * 编辑保存工艺指标
	 * @param processId
	 * @param delIds
	 * @param technicId
	 * @param code
	 * @param described
	 * @param gatherMethod
	 * @param name
	 * @param shareStat
	 * @param unit
	 * @param isInput
	 * @param quotaType
	 */
	@Transactional(rollbackFor = Exception.class)
	public void processEditSave(Integer processId, String delIds, String[] technicId, String[] code, String[] described,
			String[] gatherMethod, String[] name, String[] shareStat, String[] unit, String[] isInput,
			String[] quotaType) {
		/*删除去除工艺指标*/
		if(delIds != null && delIds.length() > 0){
			String[] delId = delIds.split(",");
			for(String dId : delId){
				TTechnicIndex technicIndex = new TTechnicIndex();
				technicIndex.setDeleteFlag("1");
				technicIndex.setId(Integer.parseInt(dId));
				tTechnicIndexMapper.updateByPrimaryKeySelective(technicIndex);
			}
		}
		boolean isDescribed = false;
		if(described != null && described.length > 0){
			isDescribed = true;
		}
		/*新增或修改工艺指标*/
		if(technicId != null && technicId.length > 0){
			for(int i = 0; i < technicId.length; i++){
				/*整理工艺指标数据*/
				TTechnicIndex technicIndex = new TTechnicIndex();
				technicIndex.setCode(code[i]);
				technicIndex.setGather_method(gatherMethod[i]);
				technicIndex.setName(name[i]);
				technicIndex.setShare_stat(shareStat[i]);
				try {
                    technicIndex.setUnit(unit[i]);
                }catch (ArrayIndexOutOfBoundsException e){
                    technicIndex.setUnit("");
                }
				technicIndex.setIsInput(isInput[i]);
				technicIndex.setQuota_type(quotaType[i]);
				if(isDescribed){
					technicIndex.setDescribed(described[i]);
				}
				if("newId".equals(technicId[i])){
					technicIndex.setDeleteFlag("0");
					technicIndex.setCreateTime(new Date());
					technicIndex.setCreatorId(UserHelper.getCurrentUser().getPersonId());
					technicIndex.setCreatorName(UserHelper.getCurrentUser().getPersonName());
					technicIndex.setDeptId(UserHelper.getCurrentDeptId());
					technicIndex.settEId(UserHelper.getCurrentCompanyId());
					technicIndex.setT_p_id(processId);
					tTechnicIndexMapper.insertSelective(technicIndex);
				} else {
					technicIndex.setId(Integer.parseInt(technicId[i]));
					tTechnicIndexMapper.updateByPrimaryKeySelective(technicIndex);
				}
			}
		}
	}

	public TProcessingTechnology queryProcessById(Integer processId) {
		/*查询工艺管理信息*/
		TProcessingTechnology tProcessingTechnology = new TProcessingTechnology();
		tProcessingTechnology.setId(processId);
		tProcessingTechnology.setDeleteFlag("0");
		tProcessingTechnology.settEId(UserHelper.getCurrentCompanyId());
		TProcessingTechnology process = tProcessingTechnologyMapper.selectByPrimaryKey(tProcessingTechnology);
		return process;
	}
	
	/**
	 * @方法名称: queryPTByRecord
	 * @功能描述: 根据
	 * @作者: 洪丰
	 * @创建时间:2018年7月16日 下午6:58:39
	 * @param record
	 * @return int
	 */
	public int queryPTByRecord(TProcessingTechnology record){
		ShiroUser user = UserHelper.getCurrentUser();
		record.setDeleteFlag("0");
		record.settEId(user.getEntId());
		List<TProcessingTechnology> ptLsit = tProcessingTechnologyMapper.select(record);
		return ptLsit.size();
	}
}
